<template>
<!-- 添加/修改学生 -->
  <div style="text-align: left">
    <el-button class="add-button" type="primary" @click="dialogFormVisible = true">添加学生信息</el-button>
    <el-dialog
      title="添加/修改学生信息"
      :visible.sync="dialogFormVisible"
      @close="clear">
      <el-form :model="form" :rules="rules" style="text-align: left" ref="dataForm">
        <el-form-item label="登录名" :label-width="formLabelWidth" prop="username">
          <el-input v-model="form.username" autocomplete="off" ></el-input>
        </el-form-item>
         <el-form-item label="学号" :label-width="formLabelWidth" prop="stuNo">
          <el-input type="number" v-model="form.stuNo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth" prop="stuName">
          <el-input v-model="form.stuName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth" prop="phone">
          <el-input type="number" v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
           <el-radio-group v-model="form.sex">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
        </el-form-item>
         <el-form-item label="地址" :label-width="formLabelWidth" prop="address">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>

         <el-form-item label="入学时间" :label-width="formLabelWidth" prop="createTime">
              <el-date-picker
                v-model="form.createTime"
                type="datetime"
                placeholder="选择日期时间"
                align="right"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions">
              </el-date-picker>
        </el-form-item>

        <!-- 引入上传图片组件 -->
        <el-form-item label="照片" :label-width="formLabelWidth" prop="imageUrl">
          <el-input v-model="form.imageUrl" autocomplete="off" placeholder="图片 URL"></el-input>
          <img-upload @onUpload="uploadImg" ref="imgUpload"></img-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import ImgUpload from '../content/ImgUpload'
  export default {
    name: 'StuAdd',
    components: {ImgUpload},
    data () {
      return {
        dialogFormVisible: false,
        username: this.$store.state.username,
        rules: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          stuName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          phone: [{ required: true, message: '电话不能为空', trigger: 'blur' }],
          sex: [{ required: true, message: '电话号码', trigger: 'blur' }],
          email: [{ required: true, message: 'E-Mail不能为空', trigger: 'blur' }],
          createTime: [{ required: true, message: '入学时间不能为空', trigger: 'blur' }],
          address: [{ required: true, message: '地址不能为空', trigger: 'blur' }]
        },
        form: {
          id: '',
          username: '',
          stuNo: '',
          stuName: '',
          phone: '',
          email: '',
          sex: '',
          address: '',
          createTime: '',
          imageUrl: ''
        },
        formLabelWidth: '120px',
        // 时间组件
       pickerOptions: {
          shortcuts: [{
            text: '今天',
            onClick (picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }, {
            text: '一周前',
            onClick (picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', date)
            }
          }]
        }
        // 时间组件结束
      }
    },
    methods: {
      clear () {
        this.$refs.imgUpload.clear()
        this.form = {
          id: '',
          username: '',
          stuNo: '',
          stuName: '',
          phone: '',
          email: '',
          sex: '',
          address: '',
          createTime: '',
          imageUrl: ''
        }
      },
      onSubmit () {
        if (this.form.id === ''){
            this.$axios
            .post('/admin/student', {
              id: this.form.id,
              username: this.form.username,
              stuNo: this.form.stuNo,
              stuName: this.form.stuName,
              phone: this.form.phone,
              email: this.form.email,
              sex: this.form.sex,
              address: this.form.address,
              createTime: this.form.createTime,
              imageUrl: this.form.imageUrl
            }).then(resp => {
              if (resp && resp.data.code === 200) {
                this.dialogFormVisible = false
                this.$emit('onSubmit')
                // 从后端返回信息
                this.$alert(resp.data.message, '提示', {
                  type: 'success',
                  dangerouslyUseHTMLString: true
                })
              } else {
                // 从后端返回错误信息
                this.$alert(resp.data.message, '错误提示', {
                type: 'error',
                dangerouslyUseHTMLString: true
                })
              }
          })
        }
        if (this.form.id !== ''){
          this.$axios
            .put('/admin/student', {
              id: this.form.id,
              username: this.form.username,
              stuNo: this.form.stuNo,
              stuName: this.form.stuName,
              phone: this.form.phone,
              email: this.form.email,
              sex: this.form.sex,
              address: this.form.address,
              createTime: this.form.createTime,
              imageUrl: this.form.imageUrl
            }).then(resp => {
              if (resp && resp.data.code === 200) {
                this.dialogFormVisible = false
                this.$emit('onSubmit')
                // 从后端返回信息
                this.$alert(resp.data.message, '提示', {
                  type: 'success',
                  dangerouslyUseHTMLString: true
                })
              } else {
                // 从后端返回错误信息
                this.$alert(resp.data.message, '错误提示', {
                type: 'error',
                dangerouslyUseHTMLString: true
                })
              }
          })
        }
      },
      uploadImg () {
        this.form.imageUrl = this.$refs.imgUpload.url
      }
    }
  }
</script>

<style scoped>
  .add-button {
     margin: 0px 0 5px 15px;
  }
</style>
